﻿//    Copyright 2014 LuoZhihui
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//      http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#region  导入名称空间

#endregion

#region  导入名称空间

using System;
using System.Collections.Generic;
using LotteryEngine.Infrastructure.Querying;
using LotteryEngine.Model;
using LotteryEngine.Model.Repositories;
using LotteryEngine.Services.Interfaces;

#endregion

namespace LotteryEngine.Services.Impl
{
	/// <summary>
	///     抽奖活动领域服务
	/// </summary>
	public class SweepstakeService : ISweepstakeService
	{
		private readonly IConfigRepository _configRepository;
		private readonly IPrizeRepository _prizeRepository;
		private readonly ISweepstakeRepository _sweepstakeRepository;

		public SweepstakeService(
			IPrizeRepository prizeRepository,
			IConfigRepository configRepository,
			ISweepstakeRepository sweepstakeRepository)
		{
			_prizeRepository = prizeRepository;
			_configRepository = configRepository;
			_sweepstakeRepository = sweepstakeRepository;
		}


		public IEnumerable<Sweepstake> GetAllSweepstake()
		{
			return _sweepstakeRepository.FindAll();
		}

		/// <summary>
		///     本活动所有的奖品列表
		/// </summary>
		public IEnumerable<Prize> GetAllPrizes(Sweepstake sweepstake)
		{
			var query = new Query();
			//query.Add(Criterion.Create<Config>(_config => _config.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			//query.Add(Criterion.Create<Config>(_config => _config.Prize.Id, sweepstake.Id, CriteriaOperator.Eq));
			//query.Add(Criterion.Create<Prize>(_prize => _prize.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			var result = _prizeRepository.FindBy(query);


			return result;
		}

		/// <summary>
		///     本活动的激活的奖品列表
		/// </summary>
		public IEnumerable<Prize> GetActivedPrizes(Sweepstake sweepstake)
		{
			var query = new Query();
			//query.Add(Criterion.Create<Prize>(_prize => _prize.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			//query.Add(Criterion.Create<Prize>(_prize => _prize.IsActived, true, CriteriaOperator.Eq));
			//query.QueryOperator = QueryOperator.And;

			var result = _prizeRepository.FindBy(query);


			return result;
		}


		/// <summary>
		///     活动今日可用的奖项配置
		/// </summary>
		public IEnumerable<Config> GetTodayActivedPrizeConfigs(Sweepstake sweepstake)
		{
			//TODO:查询有问题
			var query = new Query();
			query.Add(Criterion.Create<Config>(_config => _config.Sweepstake.Id, sweepstake.Id, CriteriaOperator.Eq));
			query.Add(Criterion.Create<Config>(_config => _config.IsActived, true, CriteriaOperator.Eq));
			query.Add(Criterion.Create<Sweepstake>(_config => _config.StartDate, DateTime.Today, CriteriaOperator.Le));
			query.Add(Criterion.Create<Sweepstake>(_config => _config.EndDate, DateTime.Today, CriteriaOperator.Le));
			query.QueryOperator = QueryOperator.And;

			var result = _configRepository.FindBy(query);

			return result;
		}
	}
}